<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>BlackStar Monitor&amp;Profile System</title>
		<script type="text/javascript" src="/static/js/jquery.min.js"></script>
		<script type="text/javascript" src="/static/js/highcharts.js"></script>
		<!--[if IE]>
			<script type="text/javascript" src="/static/js/excanvas.compiled.js"></script>
		<![endif]-->
		<script type="text/javascript">
		var preThreadCPU = ${threadCPU};
		var preProcessCPU = ${processCPU};
		var preSystemCPU = ${systemCPU};
		var interval = 20000;// updating of the chart 20 seconds
		var chart;
		function getNowTime()
		{
			var now = new Date();
			return now.getTime() - now.getTimezoneOffset() * 60 * 1000;
		}
		
		function initData()
		{
			var time = getNowTime();
			var data = [];
			var i;
			for (i = 180; i > 0; i--) {
				data.push({
					x: time - i * interval,
					y: 0
				});
			}
			return data;
		}
		
		$(document).ready(function() {
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container',
					defaultSeriesType: 'spline',
					margin: [50, 150, 60, 80],
					events: {
						load: function() {
							var seriesThread = this.series[0];
							var seriesProcess = this.series[1];
							setInterval(function() {
							    $.getJSON("cpu_json", function (data, textStatus){
							    	var curThreadCPU = data.threadCPU;
									var curProcessCPU = data.processCPU; 
							    	var curSystemCPU = data.systemCPU; 
							    	
									var x = getNowTime();
									var threadY = (curThreadCPU-preThreadCPU) / (curSystemCPU - preSystemCPU) * 100;
									threadY = new Number(threadY.toFixed(3));
									var processY = (curProcessCPU-preProcessCPU) / (curSystemCPU - preSystemCPU) * 100;
									processY = new Number(processY.toFixed(3));
									
									seriesThread.addPoint([x, threadY], true, true);
									seriesProcess.addPoint([x, processY], true, true);
									
									preThreadCPU = curThreadCPU;
									preProcessCPU = curProcessCPU;
									preSystemCPU = curSystemCPU;
								});
							}, interval);
						}
					}
				},
				title: {
					text: 'Thread CPU(Refresh Every ' + interval/1000 + ' Seconds)',
					style: {
						margin: '10px 100px 0 0' // center it
					}
				},
				xAxis: {
					type: 'datetime',
					tickPixelInterval: 150
				},
				yAxis: {
					title: {
						text: 'Percent %'
					},
					plotLines: [{
						value: 0,
						width: 1,
						color: '#808080'
					}]
				},
				plotOptions: {
					spline: {
						lineWidth: 1,
						marker: {
							enabled: false
						},
						pointInterval: 60000, // 5minute
						
						states: {
							hover: {
								marker: {
									enabled: true,
									symbol: 'circle',
									radius: 2,
									lineWidth: 1
								}
							}
						}
					}
				},
				
				
				tooltip: {
					formatter: function() {
			                return this.y + '%';
					}
				},
				legend: {
					layout: 'vertical',
					style: {
						left: 'auto',
						bottom: 'auto',
						right: '10px',
						top: '100px'
					}
				},
				series: [{
					name: 'Thread CPU',
					data: initData()
				},{
					name: 'Process CPU',
					data: initData()
				}]
			});
		});
		</script>
</head>
	<body>
		<h1><a href='/'>Index</a>&nbsp;&nbsp;<<&nbsp;&nbsp;${title}</h1>
		<h2>ThreadName : ${threadName}</h2>
		<div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>	
	</body>
</html>
